package com.example.myblog.controller;


import com.example.myblog.entity.Article;
import com.example.myblog.mapper.ArticleMapper;
import com.example.myblog.service.IArticleService;
import com.example.myblog.service.impl.ArticleServiceImpl;
import com.yc.common.vo.Result;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 廖老师
 * @since 2023-06-27
 */
@RestController
@RequestMapping("/myblog/article")
public class ArticleController {

    @Resource
    ArticleServiceImpl articleService;

    @RequestMapping("save")
    public Result save(@RequestBody @Valid Article article, Errors errors){
        if(errors.hasErrors()){
            return Result.failure("文章提交数据错误!", errors.getAllErrors());
        }
        articleService.saveOrUpdate(article);
        return Result.success("文章添加成功!");
    }

    @Resource
    ArticleMapper articleMapper;

    @RequestMapping("queryNew")
    public List<Article> queryNew(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "5") int size){
        int begin = (page  -  1) * size;
        return articleMapper.selectNew(begin,size);
    }


}

